<?php

/*
 * Following code will list all the game
 */

// array for JSON response
$response = array();


if (isset($_POST['user_id'])&&isset($_POST['id_level'])) {
    $user_id = $_POST['user_id'];
    $id_level= $_POST['id_level'];

// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

// get all game from game table
$result = mysql_query("SELECT id_level, rate, score, name, avatar,uid_friend
FROM game, user, (
SELECT uid_friend
FROM friends
WHERE friends.uid ='$user_id'
) AS fr
WHERE game.id_level = '$id_level' AND game.user_id = fr.uid_friend
AND user.user_id = game.user_id AND game.score != '1' order by score desc limit 10") or die(mysql_error());

// check for empty result
if (mysql_num_rows($result) > 0) {
    // looping through all results
    // game node
    $response["highscore"] = array();
    
    while ($row = mysql_fetch_array($result)) {
	
        // temp user array
        $highscore = array();
        $highscore["id_level"] = $row["id_level"];
        $highscore["rate"] = $row["rate"];
        $highscore["score"] = $row["score"];
		        $highscore["name"] = $row["name"];
        $highscore["avatar"] = $row["avatar"];
		$highscore["uid_friend"] = $row["uid_friend"];


        // push single product into final response array
        array_push($response["highscore"], $highscore);
    }
    // success
    $response["responseCode"] = 1;

    // echoing JSON response
    echo json_encode($response);
} else {
    // no game found
    $response["responseCode"] = -1;
    $response["responseMessage"] = "No message found";

    // echo no users JSON
    echo json_encode($response);
}
}
else {
    // required field is missing
    $response["responseCode"] = -1;
    $response["responseMessage"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>
